<template>
  <view>
    <view class="search-box"> 
    <my-search @click='goToSearch'></my-search>
    </view>
    <!-- 轮播图-->
    <swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" circular="true">
      <swiper-item v-for="(item,index) in swiperList" :key=index>
        <!-- navigator 进行页面的跳转 -->
        <navigator class="swiper-item" :url="`/subpkg/goods_detail/goods_detail?goods_id=`+item.goods_id">
          <image :src="item.image_src"></image>
        </navigator>
      </swiper-item>
    </swiper>
    <!-- 分类导航 -->
    <view class="nav-list">
      <view class="nav-item" v-for="(item,index) in navList" :key="index" @click="btnNavItem(item)">
        <image :src="item.image_src" class="nav-img"></image>
      </view>
    </view>
    <!-- 楼层区域-->
    <view class="floor-list">
      <view class="floor-item" v-for="(item,index) in floorList" :key="index">
        <!-- 楼层标题-->
        <image :src="item.floor_title.image_src" class="floor-title"></image>
        <!-- 楼层内容-->
        <view class="floor-box-img">
          <!-- 左边图片-->
          <navigator class="floor-left" :url="item.product_list[0].url">
            <image :src="item.product_list[0].image_src" :style="{width:item.product_list[0].image_width+'rpx'}"
              mode="widthFix"></image>
          </navigator>
          <!-- 右边图片-->
          <view class="floor-right-box">
            <navigator :url="item2.url" class="floor-right-img" v-for="(item2,i2) in item.product_list" :key="i2" v-if="(i2!==0)">
              <image :src="item2.image_src" :style="{width:item2.image_width + 'rpx'}" mode="widthFix"></image>
            </navigator>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        swiperList: [],
        navList: [],
        floorList: []
      };
    },
    onLoad() {
      // 小程序页面刚加载
      this.getSwiperList(),
        this.getNavList(),
        this.getFloorList()
    },
    methods: {
      /* 轮播图*/
      async getSwiperList() {
        const {
          data: res
        } = await uni.$http.get('/home/swiperdata')
        // console.log(res)
        if (res.meta.status !== 200) return uni.$showMsg()
        this.swiperList = res.message
      },
      /* 分类导航*/
      async getNavList() {
        const {
          data: res
        } = await uni.$http.get('/home/catitems')
        // console.log(res)
        if (res.meta.status !== 200) return uni.$showMsg()
        this.navList = res.message
      },
      btnNavItem(item) {
        // console.log(item)
        if (item.name == '分类') {
          uni.switchTab({
            url: '/pages/cate/cate'
          })
        }
      },
      /* 获取楼层数据 */
      async getFloorList() {
        const {
          data: res
        } = await uni.$http.get('/home/floordata')
        // console.log(res)
        if (res.meta.status !== 200) return uni.$showMsg()
        /* 处理获取过来的数据*/
        res.message.forEach(floor=>{ // 获取楼层的数据
        // 获取到商品列表的数据
          floor.product_list.forEach(prod=>{ 
          // 添加一个自定义url属性,点击要跳转的页面路径和参数
          prod.url='/subpkg/goods_list/goods_list?'+prod.navigator_url.split('?')[1]
          })
        })
        this.floorList = res.message

      },
      goToSearch(){
        uni.navigateTo({
          "url":'/subpkg/search/search'
        })
      }

    }
  }
</script>

<style lang="scss">
  swiper {
    height: 330rpx;

    .swiper-item,
    image {
      width: 100%;
      height: 100%;
    }
  }

  .nav-list {
    display: flex;
    justify-content: space-around;
    margin: 15rpx 0;

    .nav-img {
      width: 128rpx;
      height: 140rpx;
    }
  }

  .floor-title {
    height: 60rpx;
    width: 100%;
    display: flex;
  }
  .floor-box-img{
    display: flex;
    justify-content: space-around;
    .floor-left{
      padding-left: 10rpx;
    }
    .floor-right-box{
      display: flex;
      justify-content: space-around;
      flex-wrap: wrap;
    }
  }
  // 搜索框  sticky实现吸顶的效果
  .search-box{
    position: sticky;
    top: 0;
    z-index: 999;
  }
  
</style>
